package com.qitmiaojie.controller;

import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.Student;
import com.qitmiaojie.pojo.User;
import com.qitmiaojie.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    public Result students(String Name, Integer CurrentPage, Integer Size){
        return studentService.selectByPage(Name,CurrentPage,Size);
    }
    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/student")
    public Result addUser(@RequestBody Student student){
        log.info(student.toString());
        studentService.save(student);
        return new Result(200,"添加成功",null);
    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/student/{id}")
    public Result delUser(@PathVariable Integer id){
        log.info("{}",id);
        studentService.removeById(id);
        return new Result(200,"删除成功",null);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/student")
    public Result delUser(@RequestParam List<Integer> ids){
        log.info("{}",ids);
        studentService.removeByIds(ids);
        return new Result(200,"删除成功",null);
    }
    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Result findById(@PathVariable Integer id){
        return Result.success(studentService.getById(id));
    }


    /**
     * 修改
     * @param student
     * @return
     */
    @PutMapping("/student")
    public Result updateUser(@RequestBody Student student){
        log.info(student.toString());
        studentService.updateById(student);
        return Result.success("修改成功");
    }
}
